from common.base import AbstractBaseModel
from common.base import fields


class GoodsCategory(AbstractBaseModel):
    name = fields.CharField(max_length=10, description='名称')

    parent = fields.ForeignKeyField('models.GoodsCategory', related_name='subs', null=True,
                                    on_delete=fields.CASCADE, description='父类别')

    class Meta:
        table = 'tb_goods_category'
        table_description = '商品类别'

    def __str__(self):
        return self.name


class GoodsChannelGroup(AbstractBaseModel):
    """商品频道组"""
    name = fields.CharField(max_length=20, description='频道组名')

    class Meta:
        table = 'tb_channel_group'
        table_description = '商品频道组'

    def __str__(self):
        return self.name


class GoodsChannel(AbstractBaseModel):
    """商品频道"""
    group = fields.ForeignKeyField(
        'models.GoodsChannelGroup', on_delete=fields.RESTRICT, description='频道组名')
    category = fields.ForeignKeyField(
        'models.GoodsCategory', on_delete=fields.CASCADE, description='顶级商品类别')
    url = fields.CharField(max_length=50, description='频道页面链接')
    sequence = fields.IntField(description='组内顺序')

    class Meta:
        table = 'tb_goods_channel'
        table_description = '商品频道'

    def __str__(self):
        return self.category.name


class Brand(AbstractBaseModel):
    """品牌"""
    name = fields.CharField(max_length=20, description='名称')
    logo = fields.CharField(max_length=128, description='Logo图片')
    first_letter = fields.CharField(max_length=1, description='品牌首字母')

    class Meta:
        table = 'tb_brand'
        table_description = '品牌'

    def __str__(self):
        return self.name


class SPU(AbstractBaseModel):
    """商品SPU"""
    name = fields.CharField(max_length=50, description='名称')
    brand = fields.ForeignKeyField(
        'models.Brand', on_delete=fields.RESTRICT, description='品牌')
    category1 = fields.ForeignKeyField(
        'models.GoodsCategory', on_delete=fields.RESTRICT, related_name='cat1_spu', description='一级类别')
    category2 = fields.ForeignKeyField(
        'models.GoodsCategory', on_delete=fields.RESTRICT, related_name='cat2_spu', description='二级类别')
    category3 = fields.ForeignKeyField(
        'models.GoodsCategory', on_delete=fields.RESTRICT, related_name='cat3_spu', description='三级类别')
    sales = fields.IntField(default=0, description='销量')
    comments = fields.IntField(default=0, description='评价数')
    desc_detail = fields.TextField(default='', description='详细介绍')
    desc_pack = fields.TextField(default='', description='包装信息')
    desc_service = fields.TextField(default='', description='售后服务')

    class Meta:
        table = 'tb_spu'
        table_description = '商品SPU'

    def __str__(self):
        return self.name


class SKU(AbstractBaseModel):
    """商品SKU"""
    name = fields.CharField(max_length=50, description='名称')
    caption = fields.CharField(max_length=100, description='副标题')
    spu = fields.ForeignKeyField('models.SPU', on_delete=fields.CASCADE, description='商品')
    category = fields.ForeignKeyField(
        'models.GoodsCategory', on_delete=fields.RESTRICT, description='从属类别')
    price = fields.DecimalField(
        max_digits=10, decimal_places=2, description='单价')
    cost_price = fields.DecimalField(
        max_digits=10, decimal_places=2, description='进价')
    market_price = fields.DecimalField(
        max_digits=10, decimal_places=2, description='市场价')
    stock = fields.IntField(default=0, description='库存')
    sales = fields.IntField(default=0, description='销量')
    comments = fields.IntField(default=0, description='评价数')
    is_launched = fields.BooleanField(default=True, description='是否上架销售')
    default_image = fields.CharField(
        max_length=200, default='', null=True, description='默认图片')

    class Meta:
        table = 'tb_sku'
        table_description = '商品SKU'


class SKUImage(AbstractBaseModel):
    """SKU图片"""
    sku = fields.ForeignKeyField('models.SKU', on_delete=fields.CASCADE, description='sku')
    image = fields.CharField(max_length=128, description='图片')

    class Meta:
        table = 'tb_sku_image'
        table_description = 'SKU图片'


class SPUSpecification(AbstractBaseModel):
    """商品SPU规格"""
    spu = fields.ForeignKeyField('models.SPU', on_delete=fields.CASCADE,
                                 related_name='specs', description='商品SPU')
    name = fields.CharField(max_length=20, description='规格名称')

    class Meta:
        table = 'tb_spu_specification'
        table_description = '商品SPU规格'

    def __str__(self):
        return '%s: %s' % (self.spu.name, self.name)


class SpecificationOption(AbstractBaseModel):
    """规格选项"""
    spec = fields.ForeignKeyField('models.SPUSpecification', related_name='options',
                                  on_delete=fields.CASCADE, description='规格')
    value = fields.CharField(max_length=20, description='选项值')

    class Meta:
        table = 'tb_specification_option'
        table_description = '规格选项'

    def __str__(self):
        return '%s - %s' % (self.spec, self.value)


class SKUSpecification(AbstractBaseModel):
    """SKU具体规格"""
    sku = fields.ForeignKeyField('models.SKU', related_name='specs',
                                 on_delete=fields.CASCADE, description='sku')
    spec = fields.ForeignKeyField(
        'models.SPUSpecification', on_delete=fields.RESTRICT, description='规格名称')
    option = fields.ForeignKeyField(
        'models.SpecificationOption', on_delete=fields.RESTRICT, description='规格值')

    class Meta:
        table = 'tb_sku_specification'
        table_description = 'SKU规格'

    def __str__(self):
        return '%s: %s - %s' % (self.sku, self.spec.name, self.option.value)


class CategoryVisitCount(AbstractBaseModel):
    """统计分类商品访问量模型类"""
    category = fields.ForeignKeyField(
        'models.GoodsCategory', on_delete=fields.CASCADE, description='商品分类')
    count = fields.IntField(description='访问量', default=0)
    date = fields.DateField(auto_now_add=True, description='统计日期')

    class Meta:
        table = 'tb_category_visit'
        table_description = '统计分类商品访问量'


__all__ = [
    "GoodsCategory",
    "GoodsChannelGroup",
    "GoodsChannel",
    "Brand",
    "SPU",
    "SKU",
    "SKUImage",
    "SPUSpecification",
    "SpecificationOption",
    "SKUSpecification",
    "CategoryVisitCount"
]